<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>nodeoffset.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_dom_nodeoffset.js.html">nodeoffset.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2005 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Object to store the offset from one node to another in a way
<a name="line17"></a> * that works on any similar DOM structure regardless of whether it is the same
<a name="line18"></a> * actual nodes.
<a name="line19"></a> *
<a name="line20"></a> */
<a name="line21"></a>
<a name="line22"></a>goog.provide(&#39;goog.dom.NodeOffset&#39;);
<a name="line23"></a>
<a name="line24"></a>goog.require(&#39;goog.Disposable&#39;);
<a name="line25"></a>goog.require(&#39;goog.dom.TagName&#39;);
<a name="line26"></a>
<a name="line27"></a>
<a name="line28"></a>
<a name="line29"></a>/**
<a name="line30"></a> * Object to store the offset from one node to another in a way that works on
<a name="line31"></a> * any similar DOM structure regardless of whether it is the same actual nodes.
<a name="line32"></a> * @param {Node} node The node to get the offset for.
<a name="line33"></a> * @param {Node} baseNode The node to calculate the offset from.
<a name="line34"></a> * @extends {goog.Disposable}
<a name="line35"></a> * @constructor
<a name="line36"></a> */
<a name="line37"></a>goog.dom.NodeOffset = function(node, baseNode) {
<a name="line38"></a>  goog.Disposable.call(this);
<a name="line39"></a>
<a name="line40"></a>  /**
<a name="line41"></a>   * A stack of childNode offsets.
<a name="line42"></a>   * @type {Array.&lt;number&gt;}
<a name="line43"></a>   * @private
<a name="line44"></a>   */
<a name="line45"></a>  this.offsetStack_ = [];
<a name="line46"></a>
<a name="line47"></a>  /**
<a name="line48"></a>   * A stack of childNode names.
<a name="line49"></a>   * @type {Array.&lt;string&gt;}
<a name="line50"></a>   * @private
<a name="line51"></a>   */
<a name="line52"></a>  this.nameStack_ = [];
<a name="line53"></a>
<a name="line54"></a>  while (node &amp;&amp; node.nodeName != goog.dom.TagName.BODY &amp;&amp; node != baseNode) {
<a name="line55"></a>    // Compute the sibling offset.
<a name="line56"></a>    var siblingOffset = 0;
<a name="line57"></a>    var sib = node.previousSibling;
<a name="line58"></a>    while (sib) {
<a name="line59"></a>      sib = sib.previousSibling;
<a name="line60"></a>      ++siblingOffset;
<a name="line61"></a>    }
<a name="line62"></a>    this.offsetStack_.unshift(siblingOffset);
<a name="line63"></a>    this.nameStack_.unshift(node.nodeName);
<a name="line64"></a>
<a name="line65"></a>    node = node.parentNode;
<a name="line66"></a>  }
<a name="line67"></a>};
<a name="line68"></a>goog.inherits(goog.dom.NodeOffset, goog.Disposable);
<a name="line69"></a>
<a name="line70"></a>
<a name="line71"></a>/**
<a name="line72"></a> * @return {string} A string representation of this object.
<a name="line73"></a> * @override
<a name="line74"></a> */
<a name="line75"></a>goog.dom.NodeOffset.prototype.toString = function() {
<a name="line76"></a>  var strs = [];
<a name="line77"></a>  var name;
<a name="line78"></a>  for (var i = 0; name = this.nameStack_[i]; i++) {
<a name="line79"></a>    strs.push(this.offsetStack_[i] + &#39;,&#39; + name);
<a name="line80"></a>  }
<a name="line81"></a>  return strs.join(&#39;\n&#39;);
<a name="line82"></a>};
<a name="line83"></a>
<a name="line84"></a>
<a name="line85"></a>/**
<a name="line86"></a> * Walk the dom and find the node relative to baseNode.  Returns null on
<a name="line87"></a> * failure.
<a name="line88"></a> * @param {Node} baseNode The node to start walking from.  Should be equivalent
<a name="line89"></a> *     to the node passed in to the constructor, in that it should have the
<a name="line90"></a> *     same contents.
<a name="line91"></a> * @return {Node} The node relative to baseNode, or null on failure.
<a name="line92"></a> */
<a name="line93"></a>goog.dom.NodeOffset.prototype.findTargetNode = function(baseNode) {
<a name="line94"></a>  var name;
<a name="line95"></a>  var curNode = baseNode;
<a name="line96"></a>  for (var i = 0; name = this.nameStack_[i]; ++i) {
<a name="line97"></a>    curNode = curNode.childNodes[this.offsetStack_[i]];
<a name="line98"></a>
<a name="line99"></a>    // Sanity check and make sure the element names match.
<a name="line100"></a>    if (!curNode || curNode.nodeName != name) {
<a name="line101"></a>      return null;
<a name="line102"></a>    }
<a name="line103"></a>  }
<a name="line104"></a>  return curNode;
<a name="line105"></a>};
<a name="line106"></a>
<a name="line107"></a>
<a name="line108"></a>/** @override */
<a name="line109"></a>goog.dom.NodeOffset.prototype.disposeInternal = function() {
<a name="line110"></a>  delete this.offsetStack_;
<a name="line111"></a>  delete this.nameStack_;
<a name="line112"></a>};
</pre>


</body>
</html>
